package com.wuyan.locationdog.ui.my


import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.databinding.MyModifyPhoneSureActivityBinding
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.viewmodel.LoginViewModel

class MyModifyPhoneSureActivity : BaseBindingActivity<MyModifyPhoneSureActivityBinding>(), TextWatcher {
    private val mViewModel: LoginViewModel by viewModels()

    override fun initData() {
        setTitleText("更换手机号")

        binding.apply {
            etPhone.addTextChangedListener(this@MyModifyPhoneSureActivity)
            etMsg.addTextChangedListener(this@MyModifyPhoneSureActivity)

            btnMsg.setOnClickListener {
                if (etPhone.textStringTrim().isNullOrEmpty()) {
                    customShowLong("请输入验证码")
                    return@setOnClickListener
                }
                //类型 1注册 2修改密码 3登录 4修改手机号(旧手机号) 5修改手机号(新手机号) 6找回密码 7修改设备密码
                val map = HashMap<String, Any>()
                map["phone"] = etPhone.textStringTrim()
                map["type"] = 5
                mViewModel.getCode(map)
            }
            btnMyMsgHelp.setOnClickListener {
                ActivityUtils.startActivity(MyMsgHelpActivity::class.java)
            }
            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["phone"] = etPhone.textStringTrim()
                map["code"] = etMsg.textStringTrim()
                map["type"] = 5

                mViewModel.updatePhone(map)
            }
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        binding.apply {
            btnEnable(btnSure, etPhone, etMsg)
        }
    }

    private var countDownTimer: CountDownTimer? = null

    /**
     * 验证码倒计时
     */
    private fun MyModifyPhoneSureActivityBinding.msgCountDownTimer() {
        if (countDownTimer == null) {
            countDownTimer = object : CountDownTimer(60 * 1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    btnMsg.isEnabled = false
                    btnMsg.text = (millisUntilFinished / 1000).toString() + "s后重发"
                    btnMsg.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_unselected))
                }

                override fun onFinish() {
                    btnMsg.isEnabled = true
                    btnMsg.text = "获取验证码"
                    btnMsg.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_selected))

                }
            }
        }
        countDownTimer?.start()
    }

    override fun observe() {
        super.observe()

        mViewModel.yzmBean.observe(this) {
            customShowLong("成功")
            binding.msgCountDownTimer()
        }

        mViewModel.any.observe(this) {
            val map = HashMap<String, Any>()
            map["phone"] = binding.etPhone.textStringTrim()
            mViewModel.checkCode(map)
        }

        mViewModel.success.observe(this) {
            customShowLong("修改成功")
            LoginHelp.logout()
        }
    }

}